import itchat
from wordcloud import WordCloud
from matplotlib import pyplot as plt
import jieba
import re
import json
import os
import random
import math
from PIL import Image

# itchat.login()
# friends = itchat.get_friends(update=True)
# with open("data.json", "w", encoding="utf-8") as f:
#     f.write(json.dumps(friends, indent=2, ensure_ascii=False))
#
# for count, f in enumerate(friends):
#     img = itchat.get_head_img(userName=f["UserName"])
#     imgFile = open("img/" + str(count) + ".jpg", "wb")
#     imgFile.write(img)
#     imgFile.close()

x = 0
y = 0
imgs = os.listdir("img")
random.shuffle(imgs)
# 创建640*640的图片用于填充各小图片
newImg = Image.new('RGBA', (640, 640))
# 以640*640来拼接图片，math.sqrt()开平方根计算每张小图片的宽高，
width = int(math.sqrt(640 * 640 / len(imgs)))
# 每行图片数
numLine = int(640 / width)

for i in imgs:
    img = Image.open("img/" + i)
    # 缩小图片
    img = img.resize((width, width), Image.ANTIALIAS)
    # 拼接图片，一行排满，换行拼接
    newImg.paste(img, (x * width, y * width))
    x += 1
    if x >= numLine:
        x = 0
        y += 1

newImg.save("all.png")

# file = open('sign.txt', 'a', encoding='utf-8')
# for f in friends:
#     signature = f["Signature"].strip().replace("emoji", "").replace("span", "").replace("class", "")
#     # 正则匹配
#     rec = re.compile("1f\d+\w*|[<>/=]")
#     signature = rec.sub("", signature)
#     file.write(signature + "\n")

# text = open("{}.txt".format("sign"), encoding="UTF-8").read()
# wordlist = jieba.cut(text, cut_all=True)
# wl = " ".join(wordlist)
# wc = WordCloud(
#     # 设置背景颜色
#     background_color="white",
#     # 设置最大显示的词云数
#     max_words=2000,
#     # 这种字体都在电脑字体中，window在C:\Windows\Fonts\下，mac下可选/System/Library/Fonts/PingFang.ttc 字体
#     font_path='C:\\Windows\\Fonts\\simfang.ttf',
#     height=500,
#     width=500,
#     # 设置字体最大值
#     max_font_size=60,
#     # 设置有多少种随机生成状态，即有多少种配色方案
#     random_state=30,
# )
# myword = wc.generate(text)  # 生成词云 如果用结巴分词的话，使用wl 取代 text， 生成词云图
# # 展示词云图
# plt.imshow(myword)
# plt.axis("off")
# plt.show()
# wc.to_file('signature.png')  # 把词云保存下
